package com.haixiaoke.smartLock.youdian;

import com.alibaba.fastjson2.JSON;
import com.haixiaoke.vo.ydCallBackVo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 优电-事件回调
 *
 * @author xiaolong
 * @createDate: 2023年6月20日 上午9:55:50
 * @version: 1.0
 */
@Service
public class SmartLockYdCallBackService {

    private static final Logger log = LoggerFactory.getLogger(SmartLockYdCallBackService.class);
    @Autowired
    private SmartLockYdLogEventHandleService smartLockYdLogEventHandleService;

    /**
     * 优电-智能锁回调url
     *
     * @param vo
     * @return
     */
    public Map<String, Object> lockCallback(ydCallBackVo vo) {
        log.info("优电, 智能锁回调/事件url------vo={}", JSON.toJSONString(vo));
        Map<String, Object> result = new HashMap<>();
        result.put("message", "成功");
        result.put("code", 0);
        try {

            if (StringUtils.isBlank(vo.getPushType())) {
                log.warn("参数错误");
                return result;
            }

            switch (vo.getPushType()) {
                case "openDoorRecords":// 开锁日志
                    smartLockYdLogEventHandleService.logEventHandle(vo);
                    break;
                case "deviceStatusChanges":// 心跳日志
                    smartLockYdLogEventHandleService.lockOnlineCallBack(vo);
                    break;
                case "alarmRecords":// 告警日志
                    smartLockYdLogEventHandleService.lockAlarm(vo);
                    break;
                case "passwordSynchronization":// 密码同步
                    smartLockYdLogEventHandleService.lockPasswordSynchronization(vo);
                    break;
                default:
                    log.warn("优电, 智能锁回调事件处理 = {}", vo.getPushType());
                    break;
            }
        } catch (Exception e) {
            log.error("优电, 智能锁回调事件异常" + e.getMessage(), e);
        }
        return result;
    }
}
